//
//  GoodsEditViewController.m
//  FreightAssistant
//
//  Created by leo on 15/11/7.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "CommodityEditViewController.h"

#import "KKEditTableViewCell.h"

@interface CommodityEditViewController () {
    
    NSArray *_settings;
    
    NSMutableDictionary *_settingValueDictionary;
    
    KKTableFooterView *_tableFooterView;
}

@end

@implementation CommodityEditViewController

- (void)loadView {
    [super loadView];
    
    [self.tableView registerNib: [UINib nibWithNibName: NSStringFromClass([KKEditTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier: KKEditTableViewCellIdentifier];
    
    _tableFooterView = [KKTableFooterView footerViewOfSubmitButtonWithTableView: self.tableView];
    __weak typeof(_tableFooterView) weakTableFooterView = _tableFooterView;
    self.keyboardStateBlock = ^(BOOL show){
        
        weakTableFooterView.kk_submitButton.enabled = !show;
    };
    [_tableFooterView.kk_submitButton addTarget: self action: @selector(actionOfSave:) forControlEvents: UIControlEventTouchUpInside];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = _kk_commodityItem ? @"编辑商品" : @"新增商品";
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"KKCommodityEdit" ofType:@"plist"];
    _settings = [[NSArray alloc] initWithContentsOfFile:plistPath];
    _settingValueDictionary = [NSMutableDictionary dictionary];
    
    if (_kk_commodityItem) {
        
        for (NSDictionary *itemDictionary in _settings) {
            
            NSString *key = itemDictionary[@"key"];
            NSString *value = [_kk_commodityItem kk_valueForKey: key];
            
            if (key != nil && value != nil) {
                [_settingValueDictionary setValue: value forKey: key];
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

- (void)updateCellValue:(NSString*)value fromkey:(NSString*)key {
    
    [_settingValueDictionary setValue: value forKey: key];
}

#pragma mark - Action methods

- (void)actionOfSave:(UIButton*)item {
    
    NSMutableDictionary *records = [NSMutableDictionary dictionary];
    
    for (NSDictionary *itemDictionary in _settings) {
        
        NSString *key = itemDictionary[@"key"];
        NSString *value = _settingValueDictionary[key];
        NSString *error = itemDictionary[@"error"];
        
        if (value == nil && error != nil) {
            [[KKResultAlertView sharedInstance] alertViewShowMessage: error andWithSuccessful: false andWithCallBack: nil];
            return;
        }
        else {
            if (value != nil) {
                [records setValue: value forKey: key];
            }
        }
    }
    
    __weak typeof(self) weakSelf = self;
    
    if (_kk_commodityItem) {
        
        [KKDataBaseAPIInterface editCommodityWithRecords: records withCommodityIdentifier: _kk_commodityItem.kk_identifier successful:^(id object) {
            [[KKResultAlertView sharedInstance] alertViewShowMessage: @"修改成功" andWithSuccessful: true andWithCallBack:^{
                [weakSelf.navigationController popViewControllerAnimated: true];
            }];
        } faile: nil];
    }
    else {
        
        [KKDataBaseAPIInterface addCommodityWithRecords: records successful:^(id object) {

            [[KKResultAlertView sharedInstance] alertViewShowMessage: @"保存成功" andWithSuccessful: true andWithCallBack:^{
                [weakSelf.navigationController popViewControllerAnimated: true];
            }];
            
        } faile: nil];
    }
}

#pragma mark - UITableView methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _settings.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: KKEditTableViewCellIdentifier forIndexPath: indexPath];
    
    NSDictionary *cellSettingInfo = _settings[indexPath.row];
    
    [cell setSettingInfo: cellSettingInfo andWithValueInfo: _settingValueDictionary];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    __weak typeof(self) weakSelf = self;
    cell.editHandle = ^(NSString *value, NSString *key){
        [weakSelf updateCellValue: value fromkey: key];
    };
    
    return cell;
}


@end
